package com.zing.controller.admin;

import com.zing.result.Result;
import com.zing.service.ReporterService;
import com.zing.vo.OrderReportVO;
import com.zing.vo.SalesTop10ReportVO;
import com.zing.vo.TurnoverReportVO;
import com.zing.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

@RestController
@RequestMapping("/admin/report")
@Api(tags="统计报表接口")
@Slf4j
public class ReportController {
    @Autowired
    private ReporterService reporterService;

    @GetMapping("/turnoverStatistics")
    @ApiOperation("营业额统计")
    // @DateTimeFormat 是 Spring 框架的注解，用于将请求参数中控制日期和时间类型的字符串转换为对应的 Java 类型
    public Result<TurnoverReportVO> turnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ){
        TurnoverReportVO turnoverReportVO = reporterService.getTurnover(begin, end);
        return Result.success(turnoverReportVO);
    }

    @GetMapping("/userStatistics")
    @ApiOperation("用户数据量统计")
    public Result<UserReportVO> userStatistics(
            @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ){
           UserReportVO userReportVO = reporterService.getUserStatistics(begin, end);
            return Result.success(userReportVO);
    }

    @GetMapping("/ordersStatistics")
    @ApiOperation("订单数据量统计")
    public Result<OrderReportVO> ordersStatistics(
            @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ){
        OrderReportVO orderStatistics = reporterService.getOrdersStatistics(begin, end);
        return Result.success(orderStatistics);
    }

    @GetMapping("/top10")
    @ApiOperation("时间段内销量前十统计")
    public Result<SalesTop10ReportVO> salesTop10(
            @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ){
        SalesTop10ReportVO salesTop10 = reporterService.getSalesTop10(begin, end);
        return Result.success(salesTop10);
    }

    @GetMapping("/export")
    @ApiOperation("导出运营数据表")
    public void export(HttpServletResponse response){
        reporterService.exportOperateForm(response);
    }
}
